package com.mxpio.erp.purchase.policy.impl;

import com.mxpio.erp.common.enums.NoticeStatusEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.inventory.entity.WarehouseOrder;
import com.mxpio.erp.common.inventory.entity.WarehouseOrderLine;
import com.mxpio.erp.common.order.policy.AbstractNoticePolicy;
import com.mxpio.erp.common.order.policy.NoticeContext;
import com.mxpio.erp.common.purchase.entity.RejectNotice;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpio.erp.common.vo.OrderExcuteLineVo;
import com.mxpioframework.jpa.JpaUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 采购退货通知单执行策略
 * @author mxpio
 *
 */
public class RejectNoticePolicy extends AbstractNoticePolicy {

	private List<OrderExcuteLineVo> orderExcuteVos;

	public RejectNoticePolicy(List<OrderExcuteLineVo> orderExcuteVos) {
		this.orderExcuteVos = orderExcuteVos;
	}

	@Override
	public void apply(NoticeContext context) {
		if(beforeExecute(context)) {
			execute(context);
			RejectNotice notice = (RejectNotice) context.getEntity();
			String rejectType = notice.getRejectType();
			List<OrderExcuteLineVo> orderExcuteVos = context.getOrderExcuteVos();
			WarehouseOrder wo = null;
			int lineNo = 0;

			for(OrderExcuteLineVo vo : orderExcuteVos){
				lineNo++;
				if(wo == null){
					wo = new WarehouseOrder();
					wo.setAccessType(notice.getAccessType());
					wo.setBpmnStatus(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode());
					wo.setCloseStatus(OrderStatusEnums.CloseStatus.OPEN.getStatusCode());
					wo.setOrderStatus(OrderStatusEnums.OrderStatus.PLAN.getStatusCode());
					wo.setOrderDate(new Date());
					wo.setOriginBizNo(StringUtils.isBlank(vo.getBizOrderNo())?vo.getNoticeNo():vo.getBizOrderNo());//20221012 wpp 如果是由手工录入的通知单（物资系统操作），那么此处bizOrderNo会为空
					wo.setWhCode(vo.getWhCode());
					if (StringUtils.equals("0",rejectType)){//合格退货
						wo.setWoType(NoticeStatusEnums.NoticeType.PO_OUT.getCode());
					}else{//不合格退货
						wo.setWoType(NoticeStatusEnums.NoticeType.PO_DEF_OUT.getCode());
					}
					wo.setOriginNoticeNo(notice.getNoticeNo());//关联noticeno字段
					JpaUtil.save(wo);
				}
				WarehouseOrderLine line = new WarehouseOrderLine();
				line.setWhCode(wo.getWhCode());
				line.setCloseStatus(OrderStatusEnums.CloseStatus.OPEN.getStatusCode());
				Item item = JpaUtil.linq(Item.class).idEqual(vo.getItemCode()).findOne();
				line.setItemCode(vo.getItemCode());
				line.setItemGroupCode(item.getItemGroupCode());
				line.setItemName(item.getItemName());
				line.setItemSpec(item.getItemSpec());
				line.setUnitCode(item.getUnitCode());
				line.setDrawingNo(item.getDrawingNo());
				line.setQuantity(vo.getQuantity());
				line.setBizNo(wo.getBizNo());
				line.setLineNo(lineNo+"");
				line.setOriginBizNo(vo.getBizOrderNo());
				line.setOriginBizLineNo(vo.getBizOrderlineNo());
				line.setSort(vo.getSort());
				line.setLotNo(vo.getLotNo());
				line.setSubLotIndex(vo.getSubLotIndex());
				line.setOriginNoticeNo(notice.getNoticeNo());
				line.setOriginNoticeLineNo(vo.getNoticeLineNo());
				JpaUtil.save(line);
			}
			afterExecute(context);
			notice.setNoticeStatus(NoticeStatusEnums.NoticeStatus.CONFIRM.getStatusCode());
			JpaUtil.update(notice);
		} else {
			if(CollectionUtils.isEmpty(context.getErrors())) {
				context.addError("前置校验不通过");
			}
		}

	}
	@Override
	public void execute(NoticeContext context) {

	}

	@Override
	public NoticeContext config(NoticeContext context) {
		context.setOrderExcuteVos(orderExcuteVos);
		return context;
	}

	@Override
	public boolean beforeExecute(NoticeContext context) {
		return true;
	}

	@Override
	public void afterExecute(NoticeContext context) {
	}

}
